
package com.shop.cloud.mall.api.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shop.cloud.common.core.constant.SecurityConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.mall.api.service.FreightTemplatChargeService;
import com.shop.cloud.mall.api.service.FreightTemplatService;
import com.shop.cloud.mall.common.entity.FreightTemplatCharge;
import com.shop.cloud.upms.common.entity.TableArea;
import com.shop.cloud.upms.common.feign.FeignTableAreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 运费模板
 *
 * @author
 * @date 2019-12-24 16:09:31
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/freighttemplat")
@Tag(description = "freighttemplat", name = "运费模板api")
public class FreightTemplatController {
    private final FreightTemplatService freightTemplatService;
    private final FreightTemplatChargeService freightTemplatChargeService;
    private final FeignTableAreaService feignTableAreaService;

    /**
     * 获取运费模板规则
     * @param id
     * @param areaid
     * @return R
     */
	@Operation(summary = "获取运费模板规则")
    @GetMapping("/{id}/{areaid}")
    public R getFreightTemplatCharge(@PathVariable("id") String id, @PathVariable("areaid") String areaid) {
        //查出直接能匹配到地区的运费模板规则
        FreightTemplatCharge freightTemplatCharge = getChargeByAreaid(id, areaid);
        if(freightTemplatCharge!=null){
            return R.ok(freightTemplatCharge);
        }

        //无直接能匹配到地区的运费模板，则查出是否有全市级的运费模板规则
        //获取该地区的市级地区ID
        TableArea tableArea = new TableArea();
        tableArea.setId(areaid);
        areaid = getPidByAreaid(tableArea);
        //查出直接能匹配到市级的运费模板规则
        freightTemplatCharge = getChargeByAreaid(id, areaid);
        if(freightTemplatCharge!=null){
            return R.ok(freightTemplatCharge);
        }

        //无直接能匹配到市级的运费模板，则查出是否有全省级的运费模板规则
        //获取该地区的省级地区ID
        tableArea.setId(areaid);
        areaid = getPidByAreaid(tableArea);
        //查出直接能匹配到省级的运费模板规则
        freightTemplatCharge = getChargeByAreaid(id, areaid);
        if(freightTemplatCharge!=null){
            return R.ok(freightTemplatCharge);
        }

        //无直接能匹配到省级的运费模板，则查出全国默认的运费模板规则
        return R.ok(getChargeByAreaid(id, "0"));
    }

    /**
     * 根据地区Id查运费模板规则
     * @param areaid
     * @return
     */
    FreightTemplatCharge getChargeByAreaid(String freightTemplatId,String areaid){
        //查出直接能匹配到市级的运费模板规则
        List<FreightTemplatCharge> freightTemplatChargelist =
                freightTemplatChargeService.list(Wrappers.<FreightTemplatCharge>query().lambda()
                        .eq(FreightTemplatCharge::getFreightTemplatId, freightTemplatId)
                        .like(FreightTemplatCharge::getAreaIds, '"'+areaid+'"'));
        if(freightTemplatChargelist!=null && freightTemplatChargelist.size()>0){
            FreightTemplatCharge freightTemplatCharge = freightTemplatChargelist.get(0);
            freightTemplatCharge.setFreightTemplat(freightTemplatService.getById(freightTemplatId));
            return freightTemplatCharge;
        }
        return null;
    }

    /**
     * 根据地区Id查上级Id
     * @param tableArea
     * @return
     */
    String getPidByAreaid(TableArea tableArea){
        List<TableArea> tableAreaList = feignTableAreaService.getList(tableArea, SecurityConstants.FROM_IN).getData();
        if(tableAreaList!=null && tableAreaList.size()>0){
            return tableAreaList.get(0).getPid();
        }
        return null;
    }
}
